<?php

	require_once('application/config.php');
	require_once('application/controller.php');
	require_once('application/view-helper.php');
	require_once('application/repository.php');
	
	function InvokeController()
	{
		$actionName = 'index';
		$controllerName = 'home';
		$pageParameters;
		
		if ( isset($_SERVER['PATH_INFO']) )
    	{
	        $pageParameters = explode('/', $_SERVER['PATH_INFO']);
	        array_shift($pageParameters);
	        
	        $controllerName = Controller::NormalizeObjectName(array_shift($pageParameters));
	        
	        if ( sizeof($pageParameters) > 0)
	        {
	            if ( strlen($pageParameters[0]) > 0 )
	            {
	                $actionName = Controller::NormalizeObjectName(array_shift($pageParameters));
	            }
	        }
    	}
		
    	$controller = LoadController($controllerName);
    	$controller->ActionName = $actionName;
    	$controller->ControllerName = $controllerName;
    	$controller->PageParameters = $pageParameters;
    	
		return $controller;
	}
	
	function LoadController($controllerName)
	{
		
		$path = CONTROLLER_PATH.'/'.$controllerName.'.php';
		if ( file_exists($path) )
		{
			
    		include_once($path);
    		
    		$controllerClassName = Controller::QualifyObjectName($controllerName).'Controller';
    		
    		if (class_exists($controllerClassName) && get_parent_class($controllerClassName) == 'Controller')
    		{
    			$controller = new $controllerClassName();
    		
    			return $controller;
    		}
		}
		
		Do404();
	}
	
	
	

?>